package com.siyoumi.app.modules.app_book.admin;

import com.siyoumi.app.entity.BookStore;
import com.siyoumi.app.entity.BookStoreTxt;
import com.siyoumi.app.modules.account.service.SvcAccount;
import com.siyoumi.app.modules.app_book.service.SvcBookItem;
import com.siyoumi.app.modules.app_book.service.SvcBookStore;
import com.siyoumi.app.modules.app_book.service.SvcBookStoreGroup;
import com.siyoumi.app.modules.app_book.vo.*;
import com.siyoumi.app.modules.user.service.SvcSysSale;
import com.siyoumi.app.modules.user.vo.VoSaleGetMoney;
import com.siyoumi.app.service.BookStoreTxtService;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XBean;
import com.siyoumi.component.http.InputData;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.exception.EnumSys;
import com.siyoumi.util.XJson;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import com.siyoumi.validator.XValidator;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/xadmin/app_book/app_book__store__corp_pay__edit")
public class app_book__store__corp_pay__edit
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("商家-提现");
        BookStore entity = null;
        if (isStore()) {
            entity = SvcBookStore.getApp().getEntity(getStoreId());
            if (entity == null) {
                return EnumSys.ERR_VAL.getR("商家帐号异常");
            }
        } else {
            if (isAdminEdit()) {
                entity = SvcBookStore.getApp().loadEntity(getID());
            }
        }

        //扩展字段
        VaBookStoreMoneyInfo storeMoneyInfo = new VaBookStoreMoneyInfo(); //商家提现信息

        Map<String, Object> data = new HashMap<>();
        if (entity != null) {
            HashMap<String, Object> dataAppend = new LinkedHashMap<>();
            dataAppend.put("id", entity.getKey());
            //
            BookStoreTxt entityTxt = BookStoreTxtService.getBean().getEntity(entity.getKey());
            //
            //合并
            data = entity.toMap();
            data.putAll(dataAppend);

            if (XStr.hasAnyText(entityTxt.getBstoret_money_info())) { //商家提现信息
                storeMoneyInfo = XJson.parseObject(entityTxt.getBstoret_money_info(), VaBookStoreMoneyInfo.class);
            }
        }
        data.put("bstoret_money_info", storeMoneyInfo); //商家提现信息
        //余额
        BigDecimal moneyLeft = SvcSysSale.getBean().getMoneyLeft(getStoreId());
        setPageInfo("money_left", moneyLeft);
        data.put("money", moneyLeft);

        getR().setData("data", data);

        return getR();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/save")
    public XReturn save(@Validated() VoSaleGetMoney vo, BindingResult result) {
        //统一验证
        XValidator.getResult(result, true);

        BookStoreTxt entityTxt = BookStoreTxtService.getBean().getEntity(getStoreId());
        if (XStr.isNullOrEmpty(entityTxt.getBstoret_money_info())) {
            return EnumSys.MISS_VAL.getR("未配置提现信息");
        }
        VaBookStoreMoneyInfo storeMoneyInfo = XJson.parseObject(entityTxt.getBstoret_money_info(), VaBookStoreMoneyInfo.class);
        if (XStr.isNullOrEmpty(storeMoneyInfo.getUser_name())
                || XStr.isNullOrEmpty(storeMoneyInfo.getBank_name())
                || XStr.isNullOrEmpty(storeMoneyInfo.getBank_no())
        ) {
            return EnumSys.MISS_VAL.getR("提现信息未完善");
        }

        vo.setUid(getStoreId());
        vo.setApp_id("app_book");
        vo.setIsStore(true);
        vo.setTrue_name(storeMoneyInfo.getUser_name());
        vo.setBank_no(storeMoneyInfo.getBank_no());
        vo.setBank_code(storeMoneyInfo.getBank_name());
        return SvcSysSale.getBean().getMoney(vo);
    }
}